package cc.comac.mom.service.impl;

import java.util.ArrayList;
import java.util.List;

import cc.comac.mom.datahandlers.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipRepairReplacePartService;
import cc.comac.mom.bean.EquipDev;
import cc.comac.mom.bean.EquipDevAreaDef;
import cc.comac.mom.bean.EquipRepair;
import cc.comac.mom.bean.EquipRepairReplacePart;
import cc.comac.mom.bean.EquipRepairRepositorySparePartsBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
public class EquipRepairReplacePartServiceImpl implements EquipRepairReplacePartService {

	@Autowired
	private EquipRepairReplacePartDataHandler equipRepairReplacePartDataHandler;
	@Autowired
	private EquipDevDataHandler equipDevDataHandler;
	@Autowired
	private EquipRepairDataHandler equipRepairDataHandler;
	@Autowired
	private EquipDevAreaDefDataHandler equipDevAreaDefDataHandler;
	@Autowired
	private EquipRepairRepositorySparePartsDataHandler equipRepairRepositorySparePartsDataHandler;

	@Override
	public ResultBean insertBatch(@RequestBody List<EquipRepairReplacePart> list) {
		// TODO Auto-generated method stub
		return equipRepairReplacePartDataHandler.insertBatch(list);
	}

	@Override
	public ResultBean update(@RequestBody EquipRepairReplacePart obj) {
		// TODO Auto-generated method stub
		return equipRepairReplacePartDataHandler.update(obj);
	}

	@Override
	public ResultBean delete(@RequestBody EquipRepairReplacePart obj) {
		// TODO Auto-generated method stub
		return equipRepairReplacePartDataHandler.delete(obj);
	}

	@Override
	public ResultBean getPartInfo(@RequestBody EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			return null;
		}
		// 通过维修id获取故障原因id
		long faultCauseId = equipRepairDataHandler.getById(obj.getInnerData().getId()).getInnerData().getFaultCauseId();
		if (faultCauseId == 0) {
			resultBean.setErrorMessge("请先进行维修分析");
			return resultBean;
		}
		// 获取维修数据
		EquipDev equipDev = equipDevDataHandler.getById(obj.getInnerData().getDevId());
		if (equipDev == null) {
			resultBean.setErrorMessge("无法获取设备主数据信息");
			return resultBean;
		}
		long devCategoryId = equipDev.getInnerData().getDevCategoryId();
		EquipRepairRepositorySparePartsBean equipRepairRepositorySparePartsBean = new EquipRepairRepositorySparePartsBean();
		equipRepairRepositorySparePartsBean.setEquipCategoryId(devCategoryId);
		equipRepairRepositorySparePartsBean.setFaultCauseId(faultCauseId);
		equipRepairRepositorySparePartsBean.setPageNum(obj.getPageNum());
		equipRepairRepositorySparePartsBean.setPageSize(obj.getPageSize());
		equipRepairRepositorySparePartsBean.setSparePartsNo(obj.getPartNo());
		equipRepairRepositorySparePartsBean.setSparePartsName(obj.getPartName());
		equipRepairRepositorySparePartsBean.setSparePartsModel(obj.getPartModel());
		List<EquipDevAreaDef> areaNameByDevId = equipDevAreaDefDataHandler
				.getAreaNameByDevId(obj.getInnerData().getDevId());
		if (areaNameByDevId != null && areaNameByDevId.size() > 0) {
			ArrayList<Long> listIds = new ArrayList<Long>();
			for (EquipDevAreaDef item : areaNameByDevId) {
				listIds.add(item.getInnerData().getId());
			}
			equipRepairRepositorySparePartsBean.setAreaIds(listIds);

		}
		ResultDev selectByFields = equipRepairRepositorySparePartsDataHandler.selectByFields(equipRepairRepositorySparePartsBean);
		resultBean.setObject(selectByFields);
		return resultBean;
	}

	@Override
	public ResultBean deleteBatch(@RequestBody List<Long> ids) {
		// TODO Auto-generated method stub
		return equipRepairReplacePartDataHandler.deleteBatch(ids);
	}

	@Override
	public ResultBean selectByCondition(@RequestBody EquipRepairReplacePart obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev selectByCondition = equipRepairReplacePartDataHandler.selectByCondition(obj);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}

	@Override
	public ResultBean repairReplacePartDemandConfirm(@RequestBody List<EquipRepairReplacePart> repairReplacePartList) {
		ResultBean repairReplacePartDemandConfirm = equipRepairReplacePartDataHandler.repairReplacePartDemandConfirm(repairReplacePartList);
		return repairReplacePartDemandConfirm;
	}

	@Override
	public ResultBean repairReplacePartReplaceConfirm(@RequestBody List<EquipRepairReplacePart> repairReplacePartList) {
		ResultBean repairReplacePartReplaceConfirm = equipRepairReplacePartDataHandler.repairReplacePartReplaceConfirm(repairReplacePartList);
		return repairReplacePartReplaceConfirm;
	}
}
